/**
 *   Joomdiet - components/assets/javascript/energeticspend.js
 *   Author: Juan Dapena Paz (juan@bittingbits.com)
 *   Copyright (C) 2010 Bitting Bits S.L.
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

window.addEvent('domready', function() {
    function Line(id, hours, mins){
        this.id = id;
        this.hours = hours;
        this.mins = mins;
    }
    
    var lines = null;
    
    $('categoryselector').addEvent('change', function() {
        var category_id = this.get('value');
        $('category_id').set('value', category_id);
        $('task').set('value', 'loadactivities');
        $('format').set('value', 'raw');
        
        $('form1').send({
            onComplete: function (response, responseXML) {
                $('activityselector').setHTML(response);
            }
        });
    });
    
    $('addbutton').addEvent('click', function() {
        $('task').set('value', 'loaditem');
        var physicalactivity_id = $('activityselector').get('value')
        $('physicalactivity_id').set('value', physicalactivity_id);
        
        $('form1').send({
            onComplete: function (response, responseXML) {
                $('resultsbody').setHTML($('resultsbody').getHTML()+response);
                
                if (lines != null) {
                    for (var line in lines) {
                        $('physicalactivity_time_hours'+line.id).set('value', line.hours);
                        $('physicalactivity_time_mins'+line.id).set('value', line.mins);
                    }
                }
                
                if (lines == null) lines = new Array();
                var line = new Line(physicalactivity_id, 0, 0);
                lines.push(line);
                
                $$('.hours_field').addEvent('keyup', function() {
                    var id = this.get('id').substring(27);
                    item_recalc(id);
                    recalc();
                });
                
                $$('.mins_field').addEvent('keyup', function() {
                    var id = this.get('id').substring(26);
                    item_recalc(id);
                    recalc();
                });
            }
        });
    });
    
    function item_recalc(id) {
        var hours = parseFloat($('physicalactivity_time_hours'+id).get('value'));
        var mins = parseFloat($('physicalactivity_time_mins'+id).get('value'));
        
        var weightfield = parseFloat($('weightfield').get('value'));
        var gender = $('genderselector').get('value');
        var energy = parseFloat($('energy'+id).get('value'));
        
        for (line in lines) {
            if (line.id == id) {
                line.hours = hours;
                line.mins = mins;
            }
        }
        
        if (hours == '') hours = 0;
        if (mins == '') mins = 0;
        if (weightfield == '') weightfield = 0;
        
        mins = mins + (hours*60);
        var energyspend = weightfield * mins * energy;
        if (gender == 'female') {
            energyspend = energyspend - (energyspend*0.1)
        }
        $('calculateditem'+id).setHTML(energyspend);
        
        
    }
    
    function recalc() {
        //var suma = 0;
        //var elements = $$('.energy_fields');
        /*for (var element in elements) {
            suma += element.get('value');
        }*/
        // $('resultsbody').setHTML(suma)
    }
});
